package com.he.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * pagehelper物理分页插件
 *
 * @author 布衣
 */
@Configuration
public class PageHelperConfig {

    private final SqlSessionFactory sqlSessionFactory;

    public PageHelperConfig(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @PostConstruct
    public void addInterceptor() {
        Properties props = new Properties();
        props.setProperty("helperDialect", "mysql");
        props.setProperty("reasonable", "true");
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(props);
        sqlSessionFactory.getConfiguration().addInterceptor(pageInterceptor);
    }
}
